<?php

namespace App\Console\Commands;

use Illuminate\Console\Command;

class BaseSqlMigration extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'sql:migration {--TableName=-1}';

    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {
        //
        $TableName = $this->option('TableName');
        if('MWS_List_Items' == $TableName){
            $bool = true;
            while ($bool)
            {
                $bool = $this->shift41Items();
            }

        }elseif ('MWS_List_Orders' == $TableName){
            $bool = true;
            while ($bool)
            {
                $bool = $this->shift41Orders();
            }
        }elseif ('MWS_List_Orders_latest' == $TableName){
            $bool = true;
            while ($bool)
            {
                $bool = $this->shift41Orderslatest();
            }
        }elseif ('mws_list_item' == $TableName){
            $bool = true;
            while ($bool)
            {
                $bool = $this->shift248Items();
            }
        }elseif ('mws_list_order' == $TableName){
            $bool = true;
            while ($bool)
            {
                $bool = $this->shift248Orders();
            }
        }elseif ('mws_list_order_latest' == $TableName){
            $bool = true;
            while ($bool)
            {
                $bool = $this->shift248Orderslatest();
            }
        }
    }

    /**
     * 41数据库的Items表转移
     */
    public function shift41Items(){
        $newmysql = 'mysql';
        $oldmysql = 'mycat41';
        $oldtable = 'MWS_List_Items';
        $newtable = 'MWS_List_Items';
        $column = 'id';
        return $this->transferMySql($oldmysql,$newmysql,$oldtable,$newtable,$column);
    }

    /**
     * 41数据库的Orders表转移
     */
    public function shift41Orders(){
        $newmysql = 'mysql';
        $oldmysql = 'mycat41';
        $oldtable = 'MWS_List_Orders';
        $newtable = 'MWS_List_Orders';
        $column = 'id';
        return $this->transferMySql($oldmysql,$newmysql,$oldtable,$newtable,$column);
    }

    /**
     * 41数据库的Orderslaste表转移
     */
    public function shift41Orderslatest(){
        $newmysql = 'mysql';
        $oldmysql = 'mycat41';
        $oldtable = 'MWS_List_Orders_latest';
        $newtable = 'MWS_List_Orders_latest';
        $column = 'id';
        return $this->transferMySql($oldmysql,$newmysql,$oldtable,$newtable,$column);
    }

    /**
     * 248数据库的Items表转移
     */
    public function shift248Items(){
        $newmysql = 'mysql';
        $oldmysql = 'mycat248';
        $oldtable = 'mws_list_item';
        $newtable = 'mws_list_item';
        $column = 'id';
        return $this->transferMySql($oldmysql,$newmysql,$oldtable,$newtable,$column);
    }

    /**
     * 248数据库的Orders表转移
     */
    public function shift248Orders(){
        $newmysql = 'mysql';
        $oldmysql = 'mycat248';
        $oldtable = 'mws_list_order';
        $newtable = 'mws_list_order';
        $column = 'id';
        return $this->transferMySql($oldmysql,$newmysql,$oldtable,$newtable,$column);
    }

    /**
     * 41数据库的Orderslaste表转移
     */
    public function shift248Orderslatest(){
        $newmysql = 'mysql';
        $oldmysql = 'mycat248';
        $oldtable = 'mws_list_order_latest';
        $newtable = 'mws_list_order_latest';
        $column = 'id';
        return $this->transferMySql($oldmysql,$newmysql,$oldtable,$newtable,$column);
    }

    /**
     * 转移数据库工具
     * @param $oldmysql  从哪个数据库转移
     * @param $newmysql  转移到哪个数据库
     * @param $oldtable  从哪个表转移
     * @param $newtable  转移到那张表
     * @param $column  判断的字段  FetchTime
     * @param $down  字段下限
     * @param $up   字段上限
     * @param int $limit  转移的跨度
     * @return bool
     */
    /**
     * 丢失的数据库补全结构

     */
    public function transferMySql($oldmysql,$newmysql,$oldtable,$newtable,$column,$down='',$up='',$limit=5000){
        echo "start get data transferMySql".PHP_EOL;
        $result = \DB::connection($newmysql)->table($newtable)->orderByDesc($column)->first();
        if($result){
            echo $down2 = $result->$column.PHP_EOL;
            if($down != ''){
                if($down > $down2){
                    $down2 = $down;
                }
            }
            $results = \DB::connection($oldmysql)->table($oldtable)->where($column,'>',$down2)->orderBy($column)->offset(0)->limit($limit)->get();
        }else{
            if($down != ''){
                $results = \DB::connection($oldmysql)->table($oldtable)->where($column,'>',$down)->orderBy($column)->offset(0)->limit($limit)->get();
            }else{
                $results = \DB::connection($oldmysql)->table($oldtable)->orderBy($column)->offset(0)->limit($limit)->get();
            }

        }
//        print_r($results);
        if(count($results)){
            echo "get ".count($results)." data".PHP_EOL;
        }else{
            echo "转移完毕";
            return false;
        }
        $insertArray = array();
        foreach ($results as $result){
            if($up != ''){
                if($result->$column >$up){
                    echo "转移完毕".PHP_EOL;
                    break;
                }
            }
            $result = get_object_vars($result);
//            print_r($result);
            $insertArray[] = $result;
            try {
                if (count($insertArray) > 500) {
                    echo "达到500 开始插入" . PHP_EOL;
                    if (\DB::connection($newmysql)->table($newtable)->insert($insertArray)) {
                        echo "this asin insert success" . PHP_EOL;
                        $insertArray = array();
                        if($insertArray){
                            die("查看是否is null");
                        }
                    }
                }
            } catch (\Exception $e) {
                echo "join catch".PHP_EOL;
                foreach ($insertArray as $item){
                    try {
                        if (\DB::connection($newmysql)->table($newtable)->insert($item)) {
                            echo "insert success" . PHP_EOL;
                        }
                    }catch (\Exception $e){
                        echo "join catch 2;".PHP_EOL;
                        foreach ($item as $key=>$value){
                            $item[$key] = addslashes($value);
                        }
                        if (\DB::connection($newmysql)->table($newtable)->insert($item)) {
                            echo "insert success addslashes" . PHP_EOL;
                        }
                    }
                }
                return true;
            }
        }
        if (\DB::connection($newmysql)->table($newtable)->insert($insertArray)) {
            echo "this asin insert success".PHP_EOL;
            $insertArray = array();
        };
        return true;
    }



}
